<template>
  <div class="app-container">
    <el-row>
      <el-col :span="24">
        <div class="h_top">
          <div class="bread_title">
            <span>登记归还</span>
          </div>
          <div class="save_button">
            <div v-if="reviewAdmin" class="buttonBox">
              <el-button type="success" icon="el-icon-check" size="mini" @click="reviewAdminHandle">确认审核</el-button>
            </div>
            <div class="buttonBox">
              <el-button type="primary" icon="el-icon-back" size="mini" @click="goBack">返回</el-button>
            </div>
            <div class="buttonBox">
              <el-button type="primary" icon="el-icon-document-checked" size="mini" @click="save">保存</el-button>
            </div>
            <div class="buttonBox">
              <el-button type="primary" icon="el-icon-document-remove" size="mini" @click="confirmDel">删除</el-button>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
    <div>
      <el-row>
        <el-form ref="form" :model="data" :inline="true" :rules="formRules">
          <el-input v-model="data.id" type="hidden" />
          <el-row :gutter="10">
            <el-col :xl="3" :lg="4" :md="5" :sm="6">
              <el-form-item prop="coding">
                <div>编号</div>
                <el-input v-model="data.coding" size="mini" />
              </el-form-item>
            </el-col>
            <el-col :xl="4" :lg="4" :md="5" :sm="6">
              <el-form-item prop="name">
                <div>名称</div>
                <el-input v-model="data.name" size="mini" />
              </el-form-item>
            </el-col>
            <el-col :xl="3" :lg="4" :md="5" :sm="6">
              <el-form-item prop="status">
                <div>状态</div>
                <el-select v-model="data.status" size="mini" disabled>
                  <el-option v-for="(item, indexStatus) in toolHandBackStatus" :key="indexStatus" :value="indexStatus" :label="item" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :xl="3" :lg="4" :md="5" :sm="6">
              <el-form-item prop="appear_id">
                <div>出库单</div>
                <v-select v-model="data.appear_id" action="toolAppearList" :query="{kind: 1}" disabled />
              </el-form-item>
            </el-col>
            <div v-if="data.verify_admin">
              <el-col :xl="3" :lg="4" :md="5" :sm="6">
                <el-form-item>
                  <div>验证人员</div>
                  <el-input v-model="data.verify_admin.name" :disabled="true" size="mini" />
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="4" :md="5" :sm="6">
                <el-form-item>
                  <div>验证时间</div>
                  <el-date-picker v-model="data.verify_date" type="date" size="mini" :disabled="true" />
                </el-form-item>
              </el-col>
            </div>
            <el-col :xl="3" :lg="4" :md="5" :sm="6">
              <el-form-item prop="sort">
                <div>排序</div>
                <el-input v-model="data.sort" size="mini" />
              </el-form-item>
            </el-col>
            <el-col v-if="data.status === 0" :xl="3" :lg="3" :md="4" :sm="5">
              <el-form-item>
                <div v-if="data.type === 0">选择出库单</div>
                <div v-else>选择工具</div>
                <el-button v-if="data.status === 0" type="primary" size="mini" @click="drawer = true">选择</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <el-tabs>
          <el-tab-pane label="详细">
            <el-table :data="list">
              <el-table-column prop="tools_true.name" label="工具" min-width="23%" />
              <el-table-column prop="tools_true.spec" label="型号" min-width="23%" />
              <el-table-column prop="number" label="数量" min-width="18%" />
              <el-table-column prop="notice" label="备注" min-width="37%" />
              <el-table-column prop="" label="操作" min-width="9%">
                <template scope="scope">
                  <el-button v-if="data.status === 0" type="text" size="small" @click="remove(scope.row.id)">删除</el-button>
                  <div v-else>-</div>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
          <el-tab-pane label="描述">
            <el-row :gutter="10">
              <el-col :span="4">
                <div>
                  <span>创建时间日期</span>
                  <el-input v-model="data.create_time" placeholder="创建时间日期" class="mtb10" size="mini" :disabled="true" />
                </div>
                <div>
                  <span>创建人</span>
                  <el-input v-model="data.cr_admin.name" placeholder="创建人" class="mtb10" size="mini" :disabled="true" />
                </div>
                <div v-if="data.update_time">
                  <div>
                    <span>最近更新</span>
                    <el-input v-model="data.update_time" placeholder="最近更新" class="mtb10" size="mini" :disabled="true" />
                  </div>
                  <div>
                    <span>更新者</span>
                    <el-input v-model="data.up_admin.name" placeholder="更新者" class="mtb10" size="mini" :disabled="true" />
                  </div>
                </div>
              </el-col>
              <el-col :span="20">
                <Tinymce id="content" v-model="data.content" />
              </el-col>
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-row>
    </div>
    <el-drawer title="选择吧" :visible.sync="drawer" :direction="direction" :with-header="false" size="40%">
      <div class="choose-order">
        <el-form ref="tool_form" :model="tool" :inline="true" class="hf_form append_other_admin" :rules="toolRules">
          <el-input v-model="data.id" type="hidden" />
          <el-row>
            <el-col :span="12">
              <el-form-item prop="tool_id">
                <div>工具</div>
                <v-select v-model="tool.tool_id" action="toolList" :query="{appear_id:data.appear_id,add_field:'spec'}" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item prop="number">
                <div>数量</div>
                <el-input v-model="tool.number" size="mini" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-button icon="el-icon-upload2" size="mini" @click="append" />
          </el-row>
        </el-form>
        <el-table :data="appearParts">
          <el-table-column prop="tools_true.name" label="出库备件" min-width="45%" />
          <el-table-column prop="number" label="数量" min-width="15%" />
          <el-table-column prop="need_number" label="还需数量" min-width="20%" />
          <el-table-column prop="status" label="进展" min-width="20%">
            <template scope="scope">
              <span v-if="scope.row.status === 0" style="color: gray">待归还</span>
              <span v-if="scope.row.status === 1" style="color: green">已归还</span>
              <span v-if="scope.row.status === 2" style="color: #c7c432">部分归还</span>
            </template>
          </el-table-column>
        </el-table>
        <div class="mt20">
          <el-button type="primary" icon="el-icon-check" size="mini" @click="appendAllHandle">全部归还</el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import API from '@/api/tool/handBack'
import VSelect from '@/components/select'
import Tinymce from '@/components/Tinymce/index'
import { toolHandBackStatus } from '@/utils/options'
export default {
  inject: ['backUrl', 'delOne', 'update', 'appendParts', 'removeParts', 'goUrl'],
  components: {
    VSelect,
    Tinymce
  },
  data() {
    return {
      data: {
        'cr_admin': {
          'name': ''
        },
        'verify_admin': {
          'name': ''
        }
      },
      list: [],
      tool: {},
      reviewAdmin: false,
      drawer: false,
      direction: 'rtl',
      appearParts: [],
      toolHandBackStatus,
      formRules: {
        coding: [
          {
            required: true,
            message: '请输入编号',
            trigger: 'blur'
          }
        ],
        name: [
          {
            required: true,
            message: '请输入归还单名称',
            trigger: 'blur'
          }
        ],
        status: [
          {
            required: true,
            message: '请选择状态',
            trigger: 'change'
          }
        ],
        appear_id: [
          {
            required: true,
            message: '请选择出库单',
            trigger: 'change'
          }
        ],
        sort: [
          {
            required: true,
            message: '请输入排序',
            trigger: 'blur'
          }
        ]
      },
      toolRules: {
        tool_id: [
          {
            required: true,
            message: '请选择工具',
            trigger: 'change'
          }
        ],
        number: [
          {
            required: true,
            message: '请填写数量',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.index()
  },
  methods: {
    index() {
      const id = this.$route.params.id
      API.getOne(id).then(response => {
        if (response.error_code === 87404) {
          this.$alert('您请求的归还单不存在', '错误', {
            confirmButtonText: '确定',
            callback: () => {
              this.backUrl()
            }
          })
        }
        this.data = response.data
        this.list = response.data.tool_list

        if (response.data.status === 0) {
          this.reviewAdmin = true
        }

        const appearId = response.data.appear_id
        if (appearId) {
          API.getAppear(appearId).then(res => {
            if (res.data.error_code) {
              this.$alert('您请求的出库单已经不存在了', '错误', {
                confirmButtonText: '确定',
                callback: () => {
                  this.backUrl()
                }
              })
            }
            this.appearParts = res.data.tool_list
          })
        }
      })
    },
    confirmDel() {
      const id = this.data.id
      this.delOne(API, id, true)
    },
    save() {
      const thisData = this.data
      const thisForm = this.$refs.form
      this.update(API, thisData, thisForm)
    },
    append() {
      const thisForm = this.$refs.tool_form
      const tool_data = {
        'tool_id': this.tool.tool_id,
        'number': this.tool.number,
        'notice': this.tool.notice,
        'appear_id': this.data.appear_id,
        'handback_id': this.$route.params.id
      }
      const errorCode = 88004
      const errorMessage = '该工具已在归还单中，请直接更新数量'
      this.appendParts(API, thisForm, tool_data, errorCode, errorMessage)
    },
    remove(id) {
      const tool_data = {
        'id': id
      }
      this.removeParts(API, id, tool_data)
    },
    reviewAdminHandle() {
      const reviewData = {
        'status': 1,
        'content': this.data.content,
        'id': this.data.id
      }
      API.review(reviewData).then(response => {
        if (response.data.error_code === 1) {
          this.$message.success('确认！')
          this.backUrl()
        } else {
          this.$message.error(response.data.message)
        }
      })
    },
    appendAllHandle() {
      const id = this.data.appear_id
      if (id) {
        const appendAllData = {
          'appear_id': id,
          'back_id': this.$route.params.id
        }
        API.appendAll(appendAllData).then(response => {
          if (response.data.error_code === 1) {
            this.$message.success('归还成功')
            this.index()
          } else {
            this.$message.error(response.data.message)
          }
        })
      } else {
        this.$message.error('请选择出库单')
      }
    },
    goBack() {
      this.goUrl('/tool/back')
    }
  }
}
</script>

<style scoped>
.choose-order{
  width: 90%;
  margin: 20px auto;
}
.mt20{
  margin-top: 20px;
}
</style>
